Getting Started with Azure Functions (AF)

Get an overview of Azure Functions (AF).

Discussing our plan#

Azure Functions service ticks the usual managed FaaS boxes. It’ s a service that allows us to deploy and manage functions while ignoring the existence of servers.

Functions don’t run on thin air, but this time it’s Azure making sure that they are up and running. The solution scales automatically based on the load. It has monitoring, logging, debugging, security, and so on and so forth.

Note: You might have noticed that this started with the same sentences as those from the other managed FaaS chapters. That is intentional. We wanted to make it easy to compare each solution but also cover everything so nothing is missed if you only follow the examples for one solution.

All the managed FaaS solutions we’re exploring are doing, more or less, the same things, so we can skip that part and jump straight into practical hands-on examples.

Prerequisites#

Before we start deploying Azure Functions, we need to set up a few things. We need Azure credentials and a resource group.

We don’t have to have a resource group. Serverless Framework can create it for us. However, Terraform is a much better choice to create infrastructure, even if that’s limited to a single resource.

We’ve prepared a Terraform definition that will create a resource group. You can use it as is, modify it to your liking, or ignore it altogether. If you prefer to use a different option, you still might want to consult Terraform to create the resource group, or have the Serverless Framework do it for you.

First, we need to install Azure CLI (az) and Terraform and follow the instructions in the code, or roll out the requirements another way. However, we’ll also need jq. We’ll cover why we need it later on, but for now we just need to make sure we install it.

We’ll need to export the REGION, AZURE_SUBSCRIPTION_ID, and RESOURCE_GROUP variables. Their names should be self-explanatory.

Creating a cloud function#

We’ll start by creating a new Azure Functions project. Instead of using whatever Azure provides, we’ll use Serverless to create a new project. At a minimum, we need to select a programming language for our functions and a directory we want to use to keep the project files.

Although the project directory can be anything we want, languages are limited. At the time of this writing, Azure Functions support C#, JavaScript, F#, Java, PowerShell, Python, and TypeScript. By the time you read this, others might have been added to the mix, so check the official documentation. To make things a bit more complicated, the Serverless Framework might not yet have templates for all the languages Azure supports.

We’ll use Node.js (JavaScript) in our examples for a few reasons.

To begin with, it’s a widely used language. At the same time, we’ll be able to explore a language other than Go. Another critical factor is that Node.js is supported in almost all FaaS solutions, so using it will allow us to compare them easily.

Note: Don’t be discouraged if Node.js is not your language of choice. Everything we’ll do applies to any other language, as long as it’s supported by Azure Functions.

Let’s create an Azure Functions project. We’ll use the azure-nodejs template and create the files in the azure-function directory.

Creating cloud functions using serverless

Note: If you’re interested in seeing what other templates are available, you can get the list by executing serverless create --help and observing the possible values of the --template argument. Those related to Azure Functions all have the prefix “azure.”

Viewing files#

Next, we’ll enter the newly created directory azure-function, and get a list of the files that were created for us.

The output is as follows.

Files inside the azure-function directory

There are only four files in there, so this should be a quick overview.

The src directory contains a subdirectory handlers, where the source code is located. Let’s take a look at what’s inside.

The output is as follows.

Handler's directory inside azure-function

The Serverless Framework’s template comes with two sample functions. Both are nearly the same, so it should be enough just take a quick look at one of them.

The output is as follows.

Output of hello.js

There isn’t much to look at. The function responds with “Hello” and a name passed as a query or a message body. It could hardly get simpler than that.

Note: We won’t dive into extending the function to do anything more complicated than spitting out a “Hello” message. We’re assuming you know how to write an application.

The next in line is package.json.

The output is as follows.

It’s a typical npm package config that, among other things, defines the dependencies. In this case, those are serverless-azure-functions.

The serverless.yml file is the key. It defines everything Serverless needs to deploy and manage our functions.

The output, limited to the relevant parts, is as follows.

Output of serverless.yml

Most of the entries should be self-explanatory and don’t need more explanation. Later on, if you choose to use Azure Functions through the Serverless Framework, you might want to dig deeper into the available options.

Try it yourself#

You can try all of the commands used in this lesson in the code playground below. Press the “Run” button and wait for a few seconds for it to connect.

For ease of use, all of the commands above are combined in main.sh.

Please provide values for the following:
AZURE_BUCKET_NAME
Not Specified...
/
main.sh
main.tf
output.tf
variables.tf
Code playground

Viewing the Output of Google Cloud Functions (GCF)

Deploying Azure Functions (AF)